/**
 * Created with Vim7.3 ubuntu12.04
 * @fileOverview : 
 * @author : Lyle <lylechen2014@gmial.com>
 * @since : 2014-12-18 16:26:47
 * @filename : jquery.pagination.js
 * @version : v1.0 
 * @description : 翻页插件
 */
;(function($){

    $.fn.pagination = function(opt){

        this.opts = jQuery.extend({
            dom                :null,
            items_per_page     :10,  
            num_display_entries:4,
            current_page       :1,
            num_edge_entries   :1,
            prev_text          :"Prev",
            next_text          :"Next",
            ellipse_text       :"...",
            prev_show_always   :true,
            next_show_always   :true,
            total_num          :0,
            callback:function(){return false;}
        },opt||{});

    
        this.init  = function(dom) {
            if (this.opts.total_num > 0) {
                this.appendDom(dom);
            } else {
                $(dom).html('');
            }             
        };

        this.maxNumPages = function() {
            var maxNum = 0;
            if (this.opts.items_per_page!== 0) {
                maxNum = Math.ceil(this.opts.total_num/this.opts.items_per_page);
            }
            return maxNum;
        };	

        this.renderTotalDom = function() {
            var maxNum = this.maxNumPages(),
                html = '<span class="page_left_bar">total:'+maxNum+' '+this.opts.items_per_page+'/page </span>';
            return html;
        }; 

        this.getInterval = function()  {
            var ne_half = Math.ceil(this.opts.num_display_entries/2),
                maxNum = this.maxNumPages();

            var left = right = parseInt((this.opts.num_display_entries)/2);
                start = this.opts.current_page - left,
                end = this.opts.current_page + right;


            //console.log(start,end);

            if ((this.opts.num_display_entries)%2 == 0) {
                 if (this.opts.current_page <= parseInt(maxNum/2)) {
                     end = end - 1 ;
                 } else {
                     start = start + 1;
                 }
            } 

            if (start <= 0) {
                end = end - start+1;
                start = 1
            } else if(end >= maxNum) {
                start = start - (end - maxNum);
                end = maxNum;
            } 


            if (start < 0 && end > maxNum) {
                start = 1;
                end = maxNum;
            }       
            //console.log(this.opts,start,end);
            return [start,end];
        };

        this.showPageNum = function() {
            var obj = {},
                arr = this.getInterval(),
                maxNum = this.maxNumPages();

            for (var i = arr[0]; i<arr[1]+1; i++) {
                obj[i] = true;
            }

            for (var i = 1; i < (1+this.opts.num_edge_entries); i++) { 
                obj[i] = true;
            }

            for (var i = maxNum; i > (maxNum - this.opts.num_edge_entries); i--) {  
                obj[i] = true;
            }
            return obj;
        };

        this.appendDom  = function(dom) {
             var tthis = this,
                 html = this.renderPagePrevDom() + this.renderPageNumDom() + this.renderPageNextDom();
                
                 jqDom = $(html);
                 jqDom.find('a').on('click',function(){
                       tthis.clickPage(this,dom);
                 });

             $(dom).html(jqDom);
        };

        this.clickPage = function(that,dom) {
             var val = $(that).attr('val')
                 current = this.setCurrentPage(val);

             if (current !== this.opts.current_page) {
                 this.opts.current_page = parseInt(current);
                 this.opts.callback(this.opts.current_page);
                 this.appendDom(dom);
             }
        };

        this.setCurrentPage = function(val) {
            var maxNum = this.maxNumPages(),
                current = this.opts.current_page;

            if (val == 'prev') {
                current--;
            } else if (val == 'next') {
                current++;
            } else {
                current = parseInt(val);
            }

            if (current > maxNum) {
                current = maxNum;
            } else if (current < 1) {
                current = 1;
            }
            
            return current;
        };

        this.renderPageNumDom = function() { 
            var html = '',bool=true;
                maxNum = this.maxNumPages(),
                showPageObj = this.showPageNum();
                for (var i = 1 ; i<maxNum+1; i++)  {
                     if (showPageObj[i]) {
                        var dom = '';
                        if (this.opts.current_page == i) {
                            dom = this.renderDom('current',i,i);
                        } else {
                            dom = this.renderDom('',i,i);
                        }
                        html += '<span>'+dom+'</span>';
                        bool = true;
                     } else {
                         if (bool) {
                            html += '<span>'+this.opts.ellipse_text+'</span>';
                            bool = false;
                         }
                     }
                }
                return html;
        };

        this.renderPageNextDom = function() {
            var dom,
                maxNum = this.maxNumPages();
            if (this.opts.current_page == maxNum) {
                dom = this.renderDom('current',this.opts.next_text,'next');
            } else {
                dom = this.renderDom('',this.opts.next_text,'next');
            }
            return '<span class="next">'+dom+'</span>';
        };

        this.renderPagePrevDom = function() {
            var dom;
            if (this.opts.current_page == 1) {
                dom = this.renderDom('current',this.opts.prev_text,'prev');
            } else {
                dom = this.renderDom('',this.opts.prev_text,'prev');
            }
            return '<span class="prev">'+dom+'</span>';
        };

        this.renderDom = function(className,text,val) {
            return "<a val="+val+" class = "+className+" >"+text+"</a>";
        };

        var tthis = this;
        this.opts.dom.each(function(){
            tthis.init(this);
            
        });
     
        return this;
    };

})(jQuery);
